
package com.framework.auth.domain;

import com.framework.auth.handler.SortedAuthoritiesFactory;
import com.framework.auth.handler.SortedAuthoritiesFactory;
import com.framework.core.api.response.ThirdUserInfoQueryRes;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;


@Getter
@Setter
public class UserDetailsVo implements UserDetails, CredentialsContainer {

	/**   */
	private static final long serialVersionUID = -1852698082526658029L;

	private String openId;
	private String username;
	private String password;
	private String nickname;
	private String avatarUrl;
	private Integer gender;
	private String phoneNumber;
	private Integer roleId;
	private Integer authorize;
	private  Set<GrantedAuthority> authorities;


	public UserDetailsVo(ThirdUserInfoQueryRes user, Integer authorize, Collection<? extends GrantedAuthority> authorities) {
		this(user.getOpenId(), user.getNickName(), user.getAvatarUrl(), user.getGender(), authorize, authorities);
	}

	public UserDetailsVo(String openId, String nickname, String avatarUrl,
						 Integer gender, Integer authorize, Collection<? extends GrantedAuthority> authorities) {
		super();
		this.openId = openId;
		this.username = openId;
		this.nickname = nickname;
		this.avatarUrl = avatarUrl;
		this.gender = gender;
		this.authorize = authorize;
		this.authorities = Collections.unmodifiableSet(SortedAuthoritiesFactory.createSortedAuthorities(authorities));
	}


	@Override
	public void eraseCredentials() {

	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return this.authorities;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}
